package org.rzp.ideaplug.statistic.dialog;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.util.ui.UIUtil;
import lombok.Getter;
import org.jetbrains.annotations.Nullable;
import org.rzp.ideaplug.statistic.R;
import org.rzp.ideaplug.statistic.config.PushConfig;
import org.rzp.ideaplug.statistic.push.PushThread;
import org.rzp.ideaplug.statistic.setting.StatisticPushSettingState;

import javax.swing.*;
import java.awt.*;


@Getter
public class ProjectStatisticInfoDialog extends DialogWrapper {


    private JTextField jTextFieldUser, jTextFieldUrl;
    private JProgressBar jProgressBar;
    private JLabel jProcessText;
    private JTextArea jTextAreaStatistic;

    String selectedProjectName;

    public ProjectStatisticInfoDialog(@Nullable Project project, boolean canBeParent) {
        super(project, canBeParent);
        setTitle("Statistic-Push");
        init();
        setOKButtonText("Push");
    }

    @Override
    protected @Nullable JComponent createCenterPanel() {

        StatisticPushSettingState settingState = StatisticPushSettingState.getInstance();

        GridBagLayout gridBagLayout = new GridBagLayout();
        JPanel panel = new JPanel(gridBagLayout);
        panel.setPreferredSize(new Dimension(400, 250));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        JLabel j11 = new JLabel("项目 :  ", SwingConstants.RIGHT);
        Dimension dimension = new Dimension(80, 30);
        j11.setPreferredSize(dimension);
        panel.add(j11, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 3;
        gbc.gridheight = 1;

        panel.add(new JLabel(selectedProjectName = R.selectedFile.getName()), gbc);


        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        JLabel j21 = new JLabel("用户信息 :  ", SwingConstants.RIGHT);
        j21.setPreferredSize(dimension);
        panel.add(j21, gbc);


        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 3;
        gbc.gridheight = 1;
        jTextFieldUser = new JTextField(settingState.user);
        Dimension dimension1 = new Dimension(300, 30);
        jTextFieldUser.setPreferredSize(dimension1);
        panel.add(jTextFieldUser, gbc);


        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        JLabel j31 = new JLabel("提交地址 :  ", SwingConstants.RIGHT);
        j31.setPreferredSize(dimension);
        panel.add(j31, gbc);


        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 3;
        gbc.gridheight = 1;
        jTextFieldUrl = new JTextField(settingState.url);
        jTextFieldUrl.setPreferredSize(dimension1);
        panel.add(jTextFieldUrl, gbc);


        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        JLabel j41 = new JLabel("扫描进度 :  ", SwingConstants.RIGHT);
        j41.setPreferredSize(dimension);
        panel.add(j41, gbc);


        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        gbc.gridheight = 1;
        jProgressBar = new JProgressBar();
        jProgressBar.setPreferredSize(new Dimension(200, 30));
        panel.add(jProgressBar, gbc);

        gbc.gridx = 3;
        gbc.gridy = 3;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        panel.add(jProcessText = new JLabel("扫描中...... ", SwingConstants.CENTER), gbc);


        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 1;
        gbc.gridheight = 2;
        JLabel j51 = new JLabel("统计信息 :  ", SwingConstants.RIGHT);
        j51.setPreferredSize(new Dimension(80, 95));
        panel.add(j51, gbc);


        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.gridwidth = 3;
        gbc.gridheight = 2;
        jTextAreaStatistic = new JTextArea();
        jTextAreaStatistic.setEditable(false);
        //jTextAreaStatistic.setBorder(new MetalBorders.TextFieldBorder());
        jTextAreaStatistic.setPreferredSize(new Dimension(300, 60));
        panel.add(jTextAreaStatistic, gbc);

        return panel;
    }

    @Override
    protected void doOKAction() {
        String userText = jTextFieldUser.getText();
        if (null == userText || "".equals(userText.trim())) {
            Messages.showMessageDialog("用户信息不能为空!", "warning", UIUtil.getErrorIcon());
            return;
        }
        String urlText = jTextFieldUrl.getText();
        if (null == urlText || "".equals(urlText.trim())) {
            Messages.showMessageDialog("提交地址不能为空!", "warning", UIUtil.getErrorIcon());
            return;
        }
        if (null == R.statistic) {
            Messages.showMessageDialog("扫描统计中，请稍后!", "warning", UIUtil.getErrorIcon());
            return;
        }
        super.doOKAction();
        if (!urlText.equals(R.url) || !userText.equals(R.user)) {
            PushConfig.storeConfig(urlText, userText);
        }
        PushThread pushThread = new PushThread();
        new Thread(pushThread).start();
    }


    @Override
    public void setResizable(boolean resizable) {
        super.setResizable(false);
    }
}
